
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isLogin:false,
    // 存放学生信息
    studentInfo:[
      {
        studyNumber:'2021081193',
        name:'张三',
        faculty:'软件工程学院',
        phoneNumber:'18384056384',
        address:'1栋',
        door:'408'
      },
      {
        studyNumber:'2021081194',
        name:'李四',
        faculty:'软件工程学院',
        phoneNumber:'18384056384',
        address:'1栋',
        door:'408'
      },
      {
        studyNumber:'2021081195',
        name:'王五',
        faculty:'软件工程学院',
        phoneNumber:'18384056384',
        address:'1栋',
        door:'408'
      },
      {
        studyNumber:'2021081196',
        name:'张二',
        faculty:'软件工程学院',
        phoneNumber:'18384056384',
        address:'1栋',
        door:'408'
      },      
      {
        studyNumber:'2021081198',
        name:'张二',
        faculty:'软件工程学院',
        phoneNumber:'18384056384',
        address:'3栋',
        door:'408'
      },
      {
        studyNumber:'2021081198',
        name:'张二',
        faculty:'软件工程学院',
        phoneNumber:'18384056384',
        address:'3栋',
        door:'408'
      },
      {
        studyNumber:'2021081198',
        name:'张二',
        faculty:'软件工程学院',
        phoneNumber:'18384056384',
        address:'3栋',
        door:'408'
      },
    ],
    //搜索后的学习信息
    searchStudentData:[
      {
        studyNumber:'2021081193',
        name:'张三',
        faculty:'软件工程学院',
        phoneNumber:'18384056384',
        address:'1栋',
        door:'408'
      }
    ],
    // 总条数
    totalSize:1000
  },
  getters: {
    // 按宿舍分类查看
    roomInfo(state){
      let roomArr = []
      state.studentInfo.forEach(stu=>{
        roomArr.push({address:stu.address,door:stu.door})
      })
      let roomList = []
      let flag = 1
      roomArr.forEach(room=>{
        flag = 1
        roomList.forEach(item =>{
          if(room.address === item.address && room.door === item.door){
            flag = 0
          }
        })
        if(flag === 1){
          roomList.push(room)
        }
      })
      return roomList
    },
    // 将获取到的学生信息按宿舍分类
    roomData(state,getter){
      const roomList = getter.roomInfo
      
      roomList.forEach(room=>{
        room.stuList = []
        state.studentInfo.forEach(stu=>{
          if(stu.address === room.address && stu.door === room.door){
            room.stuList.push(stu)
          }
        })
      })
      return roomList
    },
  },
  mutations: {

  },
  actions: {
    // 登录（未处理）
    login(context,userInfo){
      this.state.isLogin = true
    },
    // 删除学生
    deleteStudent(context,studyNumber){
      // studyNumber 学号
      console.log(studyNumber)



      // 返回值为处理结果
      return true
    },
    // 修改学生信息
    updateStudent(context,stuInfo){
      // stuInfo 包含学生学号studyNumber,姓名name，学院 faculty，电话号phoneNumber，地址address，门牌号door
      console.log(stuInfo)



       // 返回值为处理结果
      return true
    },
    // 搜索学生信息
    searchStudent(context,studyNumber){
      // studyNumber 学号
      console.log(studyNumber)
      // 把搜索结果放在searchStudentData
      // context.state.searchStudentData.push(
      //   {
      //     studyNumber:'2021081193',
      //     name:'张三',
      //     faculty:'软件工程学院',
      //     phoneNumber:'18384056384',
      //     address:'1栋',
      //     door:'408'
      //   })
      

      // 返回值为处理结果
      return false
    },
    // 添加学生信息
    addStudent(context,stuInfo){
      // stuInfo 包含学生学号studyNumber,姓名name，学院 faculty，电话号phoneNumber，地址address，门牌号door
      console.log(stuInfo)



      // 返回值为处理结果
      return true
    },
    // 获取学生信息
    getStudentInfo(context,pageInfo){
      // pageInfo: pageSize:单页条数 pageIndex:分页页数



      // 返回值为处理结果
      return true
    }
  },
  modules: {

  }
})
